<template>
  <div class="p-4">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form inline>
          <el-form-item>
            <el-button
              type="success"
              icon="el-icon-plus"
              @click="deBounceWrap(handleAdd)"
              >新增</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="data" border>
        <el-table-column
          prop="id"
          label="索引编号"
          width="150px"
        ></el-table-column>

        <el-table-column prop="name" label="标签名称"></el-table-column>
        <el-table-column prop="use_num" label="使用数量"> </el-table-column>
        <el-table-column prop="state" label="显示状态" width="120px">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.state"
              class="switchStyle"
              :active-value="1"
              :inactive-value="0"
              active-text="启用"
              inactive-text="禁用"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="getSwitch(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="updated_at" label="编辑时间"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              plain
              size="mini"
              icon="el-icon-edit"
              @click="deBounceWrap(handleEdit, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              plain
              size="mini"
              icon="el-icon-delete"
              @click="deBounceWrap(handleDel, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <cus-pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :title="titleOptions[title]"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="40%"
      @close="handleClear"
      :before-close="handleClose"
    >
      <el-form :model="form" ref="form" :rules="rules" label-width="80px">
        <el-form-item prop="name" label="标签名称" label-width="120px">
          <el-input
            v-model="form.name"
            placeholder="请填写交友标签名称"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item prop="state" label="状态" label-width="120px">
          <el-switch
            v-model="form.state"
            class="switchStyle"
            :active-value="1"
            :inactive-value="0"
            active-text="启用"
            inactive-text="禁用"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" plain @click="handleClose">返回</el-button>
        <el-button type="primary" @click="deBounceWrap(handleSumbit, 'form')"
          >提交</el-button
        >
      </div>
    </el-dialog>

    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""
        ><em style="font-style:normal;color:red"
          >删除后会将所有使用该标签的用户标签记录删除</em
        >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  toGetList,
  addList,
  editList,
  delList
} from "@/api/operationManagement/voicetagConfiguration";
export default {
  data() {
    return {
      data: [],
      listQuery: {
        page: 1,
        page_size: 10,
        category_id: 4
      },
      total: 0,
      titleOptions: {
        add: "新增",
        edit: "编辑"
      },
      title: "add",
      rowData: {},
      form: {
        name: undefined,
        state: 1,
        category_id: 4
      },
      rules: {
        name: [
          { required: true, message: "请输入交友标签名称", trigger: "blur" },
          { max: 5, message: "字数已达上限" }
        ],

        state: [{ required: true, message: "选择状态", trigger: "change" }]
      },
      dialogVisible: false,
      //删除
      deleteDialogVisible: false,
      dataIds: undefined
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //列表
    getList() {
      toGetList(this.listQuery).then(res => {
        this.data = res.data.list;
        this.total = res.data.total;
      });
    },
    getSwitch(row) {
      let data = {
        id: row.id,
        state: row.state,
        name: row.name,
        category_id: 4
      };
      editList(data).then(res => {
        if (res.code === 200) {
          this.$message.success("修改成功");
          this.getList();
        }
      });
    },

    //新增
    handleAdd() {
      this.title = "add";
      this.dialogVisible = true;
    },

    //编辑
    handleEdit(row) {
      this.title = "edit";
      this.rowData = row;
      this.form.name = this.rowData.name;
      this.form.state = this.rowData.state;
      this.dialogVisible = true;
    },
    handleSumbit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title === "add") {
            let data = {
              state: this.form.state,
              name: this.form.name,
              category_id: 4
            };
            console.log(data, "data");
            addList(data).then(res => {
              if (res.code === 200) {
                this.$message.success("提交成功");
                this.getList();
                this.dialogVisible = false;
              } else {
              }
            });
          }
          if (this.title === "edit") {
            let data = {
              id: this.rowData.id,
              state: this.form.state,
              name: this.form.name,
              category_id: 4
            };
            editList(data).then(res => {
              if (res.code === 200) {
                this.$message.success("编辑成功");
                this.getList();
                this.dialogVisible = false;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    //清空表单
    handleClose() {
      this.dialogVisible = false;
    },
    handleClear() {
      this.$refs["form"].resetFields();
      this.form = {
        name: undefined,
        state: 1,
        category_id: 4
      };
    },
    //删除
    handleDel(row) {
      this.dataIds = { id: `${row.id}` };
      this.deleteDialogVisible = true;
    },
    // 确定删除
    confirmDelete() {
      delList(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList();
          this.deleteDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功"
          });
        }
      });
    }
  }
};
</script>
<style lang="scss">
.switchStyle .el-switch__label {
  position: absolute;
  display: none;
  color: #fff;
}
.switchStyle .el-switch__label--left {
  z-index: 9;
  left: 18px;
}
.switchStyle .el-switch__label--right {
  z-index: 9;
  left: -5px;
}
.switchStyle .el-switch__label.is-active {
  display: block;
}
.switchStyle.el-switch .el-switch__core,
.el-switch .el-switch__label {
  width: 50px !important;
}
</style>
